package com.jetgege.smartbox.Tools;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by JJT-ssd on 2017/3/30.
 */

public class AutomaticCircle  extends View {

    private float mScore1=0.0001f;
    private float mScore2=0.0001f;
    private float mScore3=0.0001f;
    //方法传进来的表示进度
    private Paint mBasePaint, mCircle1Paint,mCircle2Paint,mCircle3Paint;        //白色圆弧与黑色圆弧
    /**
     * 圆环的宽度
     */
    private int mCircleWidth=100;
    //圆弧的矩形
    private RectF mRectF;

    //白弧线度数变量
    int i = 0;


    /**
     * 要画图形，最起码要有三个对象：
     * 1.颜色对象 Color
     * 2.画笔对象 Paint
     * 3.画布对象 Canvas
     */
    @Override
    protected void onDraw(Canvas canvas) {
        initPaint();
//这是圆心的坐标，这个是在xml设置引用的这个布局控件的大小的中心点到这个布局控件的最左边
        float xy = getWidth() / 2;
        //圆圈的半径
        float radius = xy - mCircleWidth;       //改半径会造成联动的，因为围绕着mCircleWidth这个变量去开展绘制

        //初始化圆弧所需条件（及设置圆弧的外接矩形的四边）
        mRectF = new RectF();
        mRectF.set(xy - radius - mCircleWidth / 2, xy - radius - mCircleWidth / 2, xy + radius + mCircleWidth / 2, xy + radius + mCircleWidth / 2);
        //底圆弧
        canvas.drawArc(mRectF, 270f,120, false, mCircle1Paint);
        canvas.drawArc(mRectF, 30f ,120, false, mCircle2Paint);
        canvas.drawArc(mRectF, 150f,120, false, mCircle3Paint);
        //绘制弧形
        canvas.drawArc(mRectF, 270f, mScore1*0.5f, false, mBasePaint);
        canvas.drawArc(mRectF, 30f,  mScore2*0.5f, false, mBasePaint);
        canvas.drawArc(mRectF, 150f, mScore3*0.5f, false, mBasePaint);
        super.onDraw(canvas);
    }

    //暴露一个方法给外部调用来调整进度
    public void rePaint(int score1,int score2,int score3 )
    {
        if (score1==0) this.mScore1=0.000001f;
        else this.mScore1 = score1;
        if (score2==0) this.mScore2=0.000001f;
        else this.mScore2= score2;
        if (score3==0) this.mScore3=0.000001f;
        else this.mScore3 = score3;
        invalidate();
    }



    public AutomaticCircle (Context context) {super(context);}
    private void initPaint() {

        if (mBasePaint!= null) return;

        //初始底圆弧笔
        mBasePaint = new Paint();
        //设置抗锯齿，优化绘制效果的精细度
        mBasePaint.setAntiAlias(true);
        //设置图像抖动处理,也是用于优化图像的显示效果
        mBasePaint.setDither(true);
        //设置画笔的颜色
        mBasePaint.setColor(Color.parseColor("#C4DCE0"));
        //设置画笔的风格为空心
        mBasePaint.setStyle(Paint.Style.STROKE);
        //设置“空心”的外框宽度为20dp
        mBasePaint.setStrokeWidth(mCircleWidth);
        mBasePaint.setStrokeCap(Paint.Cap.ROUND);

        //初始化中心圆的黑色笔
        mCircle1Paint = new Paint();
        mCircle1Paint.setAntiAlias(true);
        mCircle1Paint.setDither(true);
        mCircle1Paint.setColor(Color.parseColor("#60DEB9"));
       // mCircle1Paint.setStrokeCap(Paint.Cap.ROUND);
        mCircle1Paint.setStyle(Paint.Style.STROKE);
        mCircle1Paint.setStrokeWidth(mCircleWidth);

        mCircle2Paint = new Paint();
        mCircle2Paint.setAntiAlias(true);
        mCircle2Paint.setDither(true);
        mCircle2Paint.setColor(Color.parseColor("#1FB5B3"));
       // mCircle2Paint.setStrokeCap(Paint.Cap.ROUND);
        mCircle2Paint.setStyle(Paint.Style.STROKE);
        mCircle2Paint.setStrokeWidth(mCircleWidth);

        mCircle3Paint = new Paint();
        mCircle3Paint.setAntiAlias(true);
        mCircle3Paint.setDither(true);
        mCircle3Paint.setColor(Color.parseColor("#018A9C"));
       // mCircle3Paint.setStrokeCap(Paint.Cap.ROUND);
        mCircle3Paint.setStyle(Paint.Style.STROKE);
        mCircle3Paint.setStrokeWidth(mCircleWidth);
    }



    public AutomaticCircle (Context context, AttributeSet attrs) {super(context, attrs );}
    public AutomaticCircle (Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle );}
}
